package com.originskyseed.platform.user.controller;

import java.math.BigDecimal;
import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import com.originskyseed.platform.user.model.LevelSystem;
import com.originskyseed.platform.user.service.ILevelSystemService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.user.i18n.UserResponseMsg;
import com.originskyseed.platform.user.model.AnchorLevel;
import com.originskyseed.platform.user.model.UserInfo;
import com.originskyseed.platform.user.service.IAnchorLevelService;
import com.originskyseed.platform.user.vo.request.AnchorLevelRequestVO;

/**
 * 主播等级体系Controller类（OSI_ANCHOR_LEVEL）
 * 
 * @author Tangwanghui
 */
@Controller
@RequestMapping("/anchorLevel")
@BaseLocaleResource(localeFileName = "i18n_user")
public class AnchorLevelController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(AnchorLevelController.class);

	@DubboReference
	private IAnchorLevelService anchorLevelService;

	@DubboReference
	private ILevelSystemService levelSystemService;

	/**
	 * 分页查询
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/anchorLevelList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<AnchorLevel>> anchorLevelList(HttpServletRequest request) {
		BaseResponseBody<BasePage<AnchorLevel>> result = null;
		try {
			// 请求对象
			AnchorLevelRequestVO requestVO = this.getParameterBean(AnchorLevelRequestVO.class);
			requestVO.setIsDel(2);
			// 分页查询
			BasePage<AnchorLevel> page = new BasePage<AnchorLevel>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.anchorLevelService.selectListByPage(requestVO.toMap(), page);
			for (AnchorLevel anchorLevel:page.getResults()) {
				LevelSystem levelSystem = this.levelSystemService.selectByPrimaryKey(anchorLevel.getSystemId());
				if(levelSystem!=null){
					anchorLevel.setSystemName(levelSystem.getTitle());
				}
			}
			result = new BaseResponseBody<BasePage<AnchorLevel>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<AnchorLevel>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/anchorLevelSave.json", method = RequestMethod.POST)
	public BaseResponseBody<AnchorLevel> anchorLevelSave(HttpServletRequest request) {
		BaseResponseBody<AnchorLevel> result = null;
		try {
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			// 请求对象
			AnchorLevel anchorLevel = this.getParameterBean(AnchorLevel.class);

			if (anchorLevel.getEmpiricValueMin().compareTo(BigDecimal.ZERO) < 0 || anchorLevel.getEmpiricValueMax().compareTo(BigDecimal.ZERO) < 0 || anchorLevel.getBasePay().compareTo(BigDecimal.ZERO) < 0) {
				return new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.VALUE_CANNOT_BE_NEGATIVE.getMsg()), false);
			} else if (anchorLevel.getEmpiricValueMax().compareTo(anchorLevel.getEmpiricValueMin()) <= 0) {
				return new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.LEVEL_EXPERIENCE_INTERVAL_ERROR.getMsg()), false);
			} else if (this.anchorLevelService.selectEmpiricValue(anchorLevel)) {
				return new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.REPEATED_EXPERIENCE_VALUE_ERROR.getMsg()), false);
			}

			if (StringUtils.isNotBlank(anchorLevel.getId())) {
				this.anchorLevelService.updateByPrimaryKey(anchorLevel);
			} else {
				anchorLevel.setCreator(userInfo.getName());
				anchorLevel.setCreatorId(userInfo.getId());
				anchorLevel.setIsDel(2);
				this.anchorLevelService.insertSelective(anchorLevel);
			}
			result = new BaseResponseBody<AnchorLevel>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_SUCCESS.getMsg()), anchorLevel);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<AnchorLevel>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/anchorLevelDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> anchorLevelDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			//this.anchorLevelService.deleteByPrimaryKeys(Arrays.asList(ids));
			this.anchorLevelService.updateByIds(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 根据id查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectAnchorLevelById.json", method = RequestMethod.POST)
	public BaseResponseBody selectAnchorLevelById(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			String id = this.getParameterString("id", null);

			// 查询
			AnchorLevel anchorLevel = this.anchorLevelService.selectByPrimaryKey(id);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_SUCCESS.getMsg()), anchorLevel);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), UserResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}
}